$(document).ready(function(){
	
	$('#hay').parallax("50%", -0.3);
	$('#hay2').parallax("50%", -0.7);
	$('#hay3').parallax("50%", -0.2);
	
	$("#callable").live("click", function(){
		callable();
	});
	
	$("#deferred").live("click", function(){
		deferred();
	});
	
	$("#accept_header").live("click", function(){
		changeAccept("application/atom+xml");
	});
	
	$("#ws").live("click", function(){
		sendWs();
	});
	
});

function callable(){
	fake_load();
	$.ajax({
        type: 'POST',
        url: location.href.substring(0, location.href.lastIndexOf("/")) + "/async/callable",
        success: function (data) {
        	success(data.name);
        }
    });
}

function deferred(){
	$.ajax({
        type: 'POST',
        url: location.href.substring(0, location.href.lastIndexOf("/")) + "/async/defer",
        timeout: 100000000000,
        success: function (data) {
        	success(data.name);
        }
    });
}

function changeAccept(contentType) {
	 var client = new XMLHttpRequest();
	 client.open("GET", location.href, false);
	 client.setRequestHeader("Accept", contentType);
	 client.send();
	 success(client.responseXML);
}

function success(data){
	$.pnotify({
	    title: 'Response',
	    text: data,
	    type: 'success'
	});
}

function sendWs() {
	 var client = new XMLHttpRequest();
	 client.open("GET", location.href.substring(0, location.href.lastIndexOf("/")) + "/webservice", false);
	 client.send();
}

function fake_load() {
    var cur_value = 1,
        progress;

    // Make a loader.
    var loader = $.pnotify({
        title: "Loading",
        text: "<div class=\"progress_bar\" />",
        icon: 'picon picon-throbber',
        hide: false,
        closer: false,
        sticker: false,
        history: false,
        before_open: function(pnotify) {
            progress = pnotify.find("div.progress_bar");
            progress.progressbar({
                value: cur_value
            });
            var timer = setInterval(function() {
                if (cur_value >= 100) {
                    // Remove the interval.
                    window.clearInterval(timer);
                    loader.pnotify_remove();
                    return;
                }
                //cur_value += Math.ceil(3 * ((100 - cur_value) / 100));
                cur_value += .2;
                progress.progressbar('option', 'value', cur_value);
            }, 2);
        }
    });
}